// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1

package fileTag

import (
	"audio-server/internal/model"
	"audio-server/internal/sqlutil"
	"context"
	"errors"

	"audio-server/internal/svc"
	"audio-server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileTagPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileTagPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileTagPageLogic {
	return &FileTagPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileTagPageLogic) FileTagPage(req *types.PageReq) (resp *types.FileTagPageRes, err error) {
	id := sqlutil.GetUserId(l.ctx)
	if id == 0 {
		return nil, errors.New("请先登录")
	}
	pageNum := req.PageNum
	if pageNum <= 0 {
		pageNum = 1
	}
	pageSize := req.PageSize
	if pageSize <= 0 {
		pageSize = 10
	}

	offset := (pageNum - 1) * pageSize
	tagModel := model.NewSysFileTagModel(l.svcCtx.Conn)
	tagList, err := tagModel.FindPage(l.ctx, offset, pageSize, id)
	if err != nil {
		return nil, err
	}

	var result []types.FileTagRes
	for _, tag := range tagList {
		tagRes := types.FileTagRes{
			TagId:      tag.TagId,
			Title:      tag.Title,
			CreateTime: tag.CreateTime.Format("2006-01-02 15:04:05"),
		}
		result = append(result, tagRes)
	}
	count, err := tagModel.FindCount(l.ctx, id)
	return &types.FileTagPageRes{
		List:  result,
		Total: int64(count),
	}, nil
}
